package com.billdowney.entity.user;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.billdowney.entity.base.BaseEntityIdByUUIDHex;
import com.billdowney.entity.base.EntityStatus;

import io.swagger.annotations.ApiModelProperty;

/**
 * 用户登录信息
 * 
 * @author 超级小富翁 - BillDowney
 * @data 2017年8月27日 下午7:13:19
 */
@Entity
@Table(name = "user_login_info")
public class UserLoginInfo extends BaseEntityIdByUUIDHex implements UserDetails {

	private static final long serialVersionUID = 6427753923821933390L;
	@ApiModelProperty(value = "手机号")
	@Column(length = 11, unique = true)
	private String phone;
	@ApiModelProperty(value = "电子邮件")
	@Column(length = 255, unique = true)
	private String email;
	@ApiModelProperty(value = "用户名")
	@Column(length = 20, unique = true)
	private String username;
	@ApiModelProperty(value = "加密密码", notes = "BCryptPasswordEncoder加密密码")
	@Column(length = 200)
	private String password;
	@ApiModelProperty(value = "原密码")
	@Column(length = 30)
	private String origPassword;

	@ManyToOne(targetEntity = UserInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "userInfo_id")
	@ApiModelProperty(value = "用户信息")
	private UserInfo userInfo;

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getOrigPassword() {
		return origPassword;
	}

	public void setOrigPassword(String origPassword) {
		this.origPassword = origPassword;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	@Transient
	Collection<? extends GrantedAuthority> authorities;

	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	// 封装了权限信息
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	// 密码信息
	@Override
	public String getPassword() {
		return password;
	}

	// 登录用户名
	@Override
	public String getUsername() {
		return username;
	}

	// 帐户是否过期
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	// 帐户是否被冻结
	@Override
	public boolean isAccountNonLocked() {
		return EntityStatus.using.equals(getStatus());
	}

	// 帐户密码是否过期，一般有的密码要求性高的系统会使用到，比较每隔一段时间就要求用户重置密码
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	// 帐号是否可用
	@Override
	public boolean isEnabled() {
		return EntityStatus.using.equals(getStatus());
	}

	@Override
	public String toString() {
		return this.username;
	}

	@Override
	public int hashCode() {
		return this.username.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return this.toString().equals(obj.toString());
	}
}
